public class Trowable {
    public static void main(String[] args) {
        func();
        System.out.println("after try catch");
    }
    public static void func() {
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }
    public static void main10(String[] args) {
        try {
            func1();
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void func1() {
        int[] arr = {1, 2, 3};
        System.out.println(arr[100]);
    }
    public static void main9(String[] args) {
        try{
            int[] arr = {1,2,3};
            arr[100] = 10;
            arr[0] = 10;
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }finally {
            System.out.println("finally中的代码一定会执行");
        }
        System.out.println("如果没有抛出异常，或者异常被处理了，try-catch后的代码也会执行");
    }
    public static void main8(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void main7(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (NullPointerException e) { // Exception可以捕获到所有异常
            e.printStackTrace();
        }catch (Exception e){ // 永远都捕获执行到
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void main6(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            // arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("这是个数组下标越界异常");
            e.printStackTrace();
        } catch (NullPointerException e) {
            System.out.println("这是个空指针异常");
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }
    public static void main5(String[] args) {
        try {
            int[] array = {1,2,3};
            System.out.println(array[3]); // 此处会抛出数组越界异常
        }catch (NullPointerException e){ // 捕获时候捕获的是空指针异常--真正的异常无法被捕获到
            e.printStackTrace();
        }
        System.out.println("后序代码");
    }
    public static int getElement(int[] array, int index){
        if(null == array){
            throw new NullPointerException("传递的数组为null");
        } if(
                index < 0 || index >= array.length){
            throw new ArrayIndexOutOfBoundsException("传递的数组下标越界");
        }
        return array[index];
    }
    public static void main4(String[] args) {
        int[] array = {1,2,3};
        getElement(array, 3);
    }
    public static void main3(String[] args) {
        int[] arr = null;
        System.out.println(arr.length);
    }
    public static void main2(String[] args) {
        int[] arr = {1, 2, 3,4,5};
        System.out.println(arr[10]);
    }
    public static void main1(String[] args) {
        System.out.println(21 / 0);
    }
}
